package com.wu.shop.module.shop.frame.view

import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.base.utils.AlertUtil
import com.wu.common.util.NumberFormatUtils
import com.wu.common.util.StatusBarUtil
import com.wu.common.util.StringUtil
import com.wu.common.util.TextViewUtils
import com.wu.shop.R
import com.wu.shop.module.shop.model.GoodsInfo
import com.wu.shop.module.shop.model.ShopCarInfo
import com.wu.shop.module.shop.ui.adapter.ShopCarAdapter
import com.wu.shop.module.shop.ui.fragment.ShopCarFragment
import com.wu.shop.observable.shopcar.ShopCarBuyInfo
import com.wu.shop.util.ShopCarUtil

class ShopCarView(fragment: ShopCarFragment) : MvpView {

    var mFragment = fragment
    var imgUrl = "https://img0.baidu.com/it/u=3101694723,748884042&fm=26&fmt=auto&gp=0.jpg"
    var mAdapter: ShopCarAdapter? = null
    var shopList = ArrayList<ShopCarInfo>();
    fun initView() {
        var hight = StatusBarUtil.getStatusBarHeight(mFragment.activity);
        mFragment.binding.tvTitle.setPadding(0, hight + 20, 0, 40)
        mFragment.binding.tvEdit.setPadding(0, hight + 20, 40, 40)

        mFragment.binding.onClick = mFragment
        mFragment.binding.rvContent.layoutManager = LinearLayoutManager(mFragment.activity)

        mAdapter = ShopCarAdapter(mFragment.activity!!)
        mFragment.binding.rvContent.adapter = mAdapter

        var goodsList = ArrayList<GoodsInfo>();
        for (index in 1..5) {
            var goodsInfo = GoodsInfo()
            goodsInfo.isChecked = false
            goodsInfo.goodsIcon = imgUrl
            goodsInfo.goodsId = index.toString()
            goodsInfo.goodsNum = 10
            goodsInfo.goodsPrice = "10.1"
            goodsInfo.shopId = "1"
            goodsList.add(goodsInfo)
        }
        var goodsList2 = ArrayList<GoodsInfo>();
        for (index in 1..5) {
            var goodsInfo = GoodsInfo()
            goodsInfo.isChecked = false
            goodsInfo.goodsIcon = imgUrl
            goodsInfo.goodsId = (index + 10).toString()
            goodsInfo.goodsNum = 10
            goodsInfo.goodsPrice = "10.1"
            goodsInfo.shopId = "2"
            goodsList2.add(goodsInfo)
        }


//        var info2 = ShopCarInfo()
//        info2.shopName = "天猫"
//        info2.isSelect = false
//        info2.goodsList = goodsList
//        info2.shopId = "2"

        var info = ShopCarInfo()
        info.shopName = "京东"
        info.isSelect = false
        info.goodsList = goodsList
        info.shopId = "1"
        shopList.add(info)

        var info2 = ShopCarInfo()
        info2.shopName = "京东"
        info2.isSelect = false
        info2.goodsList = goodsList2
        info2.shopId = "2"
        shopList.add(info2)
        mAdapter!!.addItems(shopList)
    }

    /**
     * 加减
     */
    fun addOrSub(info: ShopCarBuyInfo) {
        if (info == null) return
        val discountResult =
            StringUtil.saveTwoDecimal(info.goodsNum * NumberFormatUtils.toDouble(info.goodsPrices))
        val oldMoney = mFragment.binding.tvMoney.text.toString()
        var money: String = ""
        if (info.isAdd) {
            money = StringUtil.add(oldMoney, discountResult).toString()
            mFragment.buyIds.add(info.goodsId)

        } else {
            money = StringUtil.sub(oldMoney, discountResult).toString()
            mFragment.buyIds.remove(info.goodsId)
        }
        TextViewUtils.setTextViewPrice(mFragment.binding.tvMoney, money)
        processCheckBox(info)
    }

    private fun processCheckBox(info: ShopCarBuyInfo) {
        if (info.isAdd) {
            processAddCheck(info)
        } else {
            processSubCheck(info)
        }
        var isAll = true;
        mAdapter!!.getItems()!!.forEach {
            if (!it.isSelect) {
                isAll = false
            }
        }
        showCheckAll(isAll);
    }

    private fun processAddCheck(info: ShopCarBuyInfo) {
        var position = -1
        mAdapter!!.getItems()!!.forEachIndexed { index, shopCarInfo ->
            if (info.shopId.equals(shopCarInfo.shopId)) {
                position = index
                return@forEachIndexed
            }
        }
        if (position != -1) {
            var isAll = true
            mAdapter!!.getItem(position)!!.goodsList.forEach {
                if (!it.isChecked) {
                    isAll = false
                    return@forEach
                }
            }
            mAdapter!!.getItem(position)!!.isSelect = isAll
            mAdapter!!.notifyItemChanged(position)
        }
    }

    private fun processSubCheck(info: ShopCarBuyInfo) {
        var position = -1
        mAdapter!!.getItems()!!.forEachIndexed { index, shopCarInfo ->
            if (info.shopId.equals(shopCarInfo.shopId)) {
                position = index
                return@forEachIndexed
            }
        }
        if (position != -1) {
            mAdapter!!.getItem(position)!!.isSelect = false
            mAdapter!!.notifyItemChanged(position)
            showCheckAll(false)
            processBuyId()
        }
    }

    /**
     * 迭代器删除
     */
    fun processDelete() {
        var shopCarLists = mAdapter!!.getItems();
        var shopIterator = shopCarLists!!.listIterator()
        while (shopIterator.hasNext()) {
            var shopInfo = shopIterator.next()
            if (shopInfo.isSelect) {
                shopInfo.goodsList.forEach {
                    if (it.isChecked) {
                        ShopCarUtil.processAddOrSub(
                            it.goodsId,
                            it.goodsPrice,
                            false,
                            it.goodsNum
                        )
                    }
                }
                shopIterator.remove()
            } else {
                var goodsIterator = shopInfo.goodsList.listIterator()
                while (goodsIterator.hasNext()) {
                    var goodsInfo = goodsIterator.next()
                    mFragment.buyIds.remove(goodsInfo.goodsId)
                    if (goodsInfo.isChecked) {
                        ShopCarUtil.processAddOrSub(
                            goodsInfo.goodsId,
                            goodsInfo.goodsPrice,
                            false,
                            goodsInfo.goodsNum
                        )
                        goodsIterator.remove()
                    }
                }
            }
        }
        mAdapter!!.notifyDataSetChanged()
        processBuyId()
    }

    // 结算
    fun processSettlement() {
        showMessage("结算")
    }

    fun showMessage(message: String) {
        if (TextUtils.isEmpty(message) || mFragment.isHidden) return
        AlertUtil.showDeftToast(mFragment.activity, message)

    }

    /**
     * 处理编辑全选
     */
    fun processCheck() {
        if (mAdapter == null || mAdapter!!.getItems() == null) return
        mFragment.isCheckedAll = !mFragment.isCheckedAll
        showCheckAll(mFragment.isCheckedAll)
        mAdapter!!.getItems()!!.forEachIndexed { index, shopCarInfo ->
            if (shopCarInfo.goodsList == null) return
            shopCarInfo.isSelect = mFragment.isCheckedAll
            shopCarInfo.goodsList.forEach {
                if (it == null) return
                it.isChecked = mFragment.isCheckedAll
                ShopCarUtil.processAddOrSub(
                    it.goodsId,
                    it.goodsPrice,
                    it.isChecked,
                    it.goodsNum
                )
            }
        }
        mAdapter!!.notifyDataSetChanged()
    }

    //添加
    fun addBuyId(goodsId: String) {
        if (mFragment.buyIds == null || TextUtils.isEmpty(goodsId)) return
        mFragment.buyIds.add(goodsId)
    }

    //删除
    fun removeBuyId(goodsId: String) {
        if (mFragment.buyIds == null || TextUtils.isEmpty(goodsId)) return
        mFragment.buyIds.remove(goodsId)
    }

    /**
     * 处理选中的Id
     */
    fun processBuyId() {
        if (mAdapter == null || mAdapter!!.getItems() == null) return
        if (mFragment.buyIds == null) mFragment.buyIds = ArrayList<String>()
        mFragment.buyIds.clear()
        mAdapter!!.getItems()!!.forEach {
            if (it.goodsList == null) return
            it.goodsList.forEach {
                if (it.isChecked) {
                    mFragment.buyIds.add(it.goodsId)
                }
            }
        }
    }

    /**
     * 展示全部选中
     */
    private fun showCheckAll(checkedAll: Boolean) {
        if (checkedAll) {
            mFragment.binding.cbAll.setBackgroundResource(R.drawable.iv_shop_select)
        } else {
            mFragment.binding.cbAll.setBackgroundResource(R.drawable.iv_shop_defult)
        }
    }

}
